#import BlackBoard.Activity;
import random;
import BlackBoard.InputSim;
import stgm;
from subprocess import call;

bReadXML = True;
# sXmlFile = "./BlackBoard/XmlActs/ActsHmmTest.xml";
nSeed = 3;
inputSim = BlackBoard.InputSim(stgm, nSeed, nTimes=3, bReadXML=bReadXML, sXmlFile="");
#inputSim.printTransitionM();
#inputSim.printPriors();
#inputSim.printStateSpace();
#print "";
# actT0 = [["Chase"], ["*", "P2"]];
# actT1 = [["Chase"], ["P3", "P2"]];
# print inputSim.getIndex(actT0, actT1);
# print inputSim.getIndexPrior(actT1);

# inputSim.makeXmlActivities("./BlackBoard/XmlActs/test.xml");
# inputSim.makeXmlActivities("./BlackBoard/XmlActs/ActsHmmAll.xml");


blackB = BlackBoard.BlackBoard(inputSim);
blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/run000.xml");
blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/chase-first000.xml");
blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/chase-second000.xml");

# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/chase-first001.xml"); #No-Score
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/chase-second001.xml"); #No-Score

# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test000.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test003.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test004.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test005.xml");

# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test001.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test002.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/xml_BB_Tests/test006.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Acts_RunChase_Debug_Small.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/Acts_RunChase_Debug_Small2.xml");
# print "aKeys_Time:", blackB.aKeys_Time;
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmTest.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmTest1.xml");
# blackB.addXmlActs_ToBB("./BlackBoard/XmlActs/ActsHmmTest2.xml");
#if bReadXML==False: inputSim.printActivities();
#blackB.graph.setup();
# aActSeq1 = ["300.15", "50.35", "50.60", "300.85", "300.100"];
# aActSeq2 = ["300.15", "300.35", "300.60", "300.85", "300.100"];
# aActSeq3 = ["300.15", "300.35", "800.60", "800.85", "300.100"];
# aActSeq4 = ["300.15", "50.35", "800.60", "300.85", "300.100"];
# aActSeqs = [aActSeq1, aActSeq2, aActSeq3, aActSeq4];
# print "getProbSeq:", blackB.getProbSeq(aActSeq1);
# print blackB.getMaxProbSeq(aActSeqs);
# print "StateProb:", blackB.getStateProb("60", [0,1,1,0,0]);
# blackB.setEvObs();
# blackB.setMExObs();


#print "\nRemoving Nodes:";
#blackB.removeAct("00");
#blackB.removeAct("300");
#blackB.removeAct("300");
#print blackB.getMaxStProb("60");
# print blackB.getMaxProbSeq();
blackB.getHmms(bSmooth=True);
# blackB.removeAct("800");
# blackB.removeAct("300");
# blackB.removeAct("64");
# blackB.removeAct("58");

# blackB.makeUsrSegment(65);
# blackB.removeUsrSegment(65);


# #Print Posterior Probs for States at "t"
# aTimes = [15, 35, 60, 85, 100];
# print "\n---";
# for nT in aTimes:
#     [aNodes, aPosts, sT] = blackB.getStatePosts(nT, nT);
#     print "\n--State Posts of time:", sT;
#     for i,node in enumerate(aNodes):
#         print node.name+":", aPosts[i];

# # [aNodes, aPosts, sT] = blackB.getStatePosts(90, 100); #Run:34,34 Chase:80,80
# # print "\n--State Posts of time:", sT;
# # for i,node in enumerate(aNodes):
# #     print node.name+":", aPosts[i];
    
# # aActSeq1 = ["800.50", "800.90"];
# # aActSeq2 = ["50.50", "Null_A"];
# # nProbSeq1 = blackB.getProbSeq(aActSeq1);
# # nProbSeq2 = blackB.getProbSeq(aActSeq2);
# # print "---\ngetProbSeq1:", nProbSeq1;
# # print "getProbSeq2:", nProbSeq2;
# # 
# print blackB.getMaxProbSeq();
# # print "---\ngetProbSeq1:", nProbSeq1;


# # #Print Joint Probs for States at "t"
# # [aNodes, aPosts, sT] = blackB.getJointProb(15, 100);
# # print "\n---";
# # print "Joint of Nodes at time:", sT;
# # for node in aNodes:
# #     print node.name;
# # print "Joint States & Probs:"; 
# # for i,nP in enumerate(aPosts):
# #     print bin(i)[2:].zfill(len(aNodes))+", Prob:", nP;

# print "\nGraph Info:"
# blackB.printGraph();


# # aNodes = [];
# # i = 0;
# # print "";
# # blackB.setMExObs();
# # blackB.setEvObs();
# # for sK in blackB.aActs_Time:
# #     for j in range(len(blackB.aActs_Time[sK])):
# #         aNodes.append(blackB.aActs_Time[sK][j].rootAct);
# #         print blackB.aActs_Time[sK][j].rootAct.name;
# #     print i+1, "Priors:", blackB.graph.priorprob(aNodes);
# #     print i+1, "Posts:", blackB.graph.postprob(aNodes);
# #     i += 1;
# #     aNodes = [];
 
blackB.graph.write2dot(fname='./BlackBoard/DynBB-graph.dot');
call(['dot', '-Tpdf', './BlackBoard/DynBB-graph.dot', '-o', './BlackBoard/DynBB-graph.pdf']);
call(['evince', './BlackBoard/DynBB-graph.pdf']);
